package com.ksd.api.controller;

import com.ksd.api.beans.*;
import com.ksd.api.tool.VodDataRecommend;
import com.ksd.api.utils.HttpServletUtil;
import com.ksd.common.config.PlayControlConfig;
import com.ksd.common.core.controller.BaseController;
import com.ksd.common.core.domain.AjaxResult;
import com.ksd.common.utils.DateUtils;
import com.ksd.common.utils.StringUtils;
import com.ksd.framework.web.domain.server.Sys;
import com.ksd.system.domain.*;
import com.ksd.system.service.*;
import com.ksd.system.service.impl.SysTvDataServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@RestController
public class GetSongList extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(GetSongList.class);
    @Autowired
    private ISysMusicDataService sysMusicDataService;
    @Autowired
    private ISysBoardService sysBoardService;



    @RequestMapping(value = "/api/user/vod/getSongList", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult getTvType(HttpServletRequest httpServletRequest) {
        logger.info("===================指定歌曲分类,获取该类型下的所有专辑列表===============");
        HttpServletUtil.getRequestParam(httpServletRequest);

        String keyword = httpServletRequest.getParameter("keyword");
        String cid = httpServletRequest.getParameter("cat_id");


        GetSongListResponse getSongListResponse = new GetSongListResponse();
        List<SongData> songDataList = new ArrayList<>();
        SysMusicData sysMusicData = new SysMusicData();
        SysTvData sysTvData = new SysTvData();
        if (StringUtils.isNotEmpty(cid)) {
            sysMusicData.setTypeData(Long.valueOf(cid));  //音乐类型:流行/摇滚
        }
        if (StringUtils.isNotEmpty(keyword)) {
            sysMusicData.setKeyWord(keyword);
        }
        List<SysMusicData> sysMusicDataList = sysMusicDataService.selectSysMusicDataList(sysMusicData);
        SysBoard sysBoard = sysBoardService.selectSysBoardById(SysBoard.serviceFileId);
        String serviceIpUrl = sysBoard.getUrl();
        logger.info("该类型下的专辑个数:"+sysMusicDataList.size());
        for (SysMusicData result : sysMusicDataList) {
            SongData songData = new SongData();
            songData.setAutoid(Integer.valueOf(result.getId().toString()));
            songData.setTitle(result.getTitle());
            songData.setIs_recommend(VodDataRecommend.IS.getResult());
            songData.setRecommend_banner(result.getImgUrl());
            songData.setSource("");
            songData.setImg_address(serviceIpUrl+result.getImgUrl());
            songData.setStatus(0);
            songData.setUrl_address(serviceIpUrl+result.getImgUrl());
            songData.setFile_type("music");
            songData.setCreated_at(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,result.getCreateTime()));
            songData.setIs_now(0);
            songData.setUrlAddress(serviceIpUrl+result.getImgUrl());
            songData.setLogo(serviceIpUrl+result.getImgUrl());
            songData.setCat_id(result.getId().toString());

            songDataList.add(songData);
        }
        getSongListResponse.setData(songDataList);
        getSongListResponse.setCurrent_page(1);
        getSongListResponse.setFrom(1);
        getSongListResponse.setPer_page("1000");
        getSongListResponse.setTo(songDataList.size());
        getSongListResponse.setTotal(songDataList.size());

        return AjaxResult.success(getSongListResponse);
    }

}